{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "57484ac0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import csv\n",
    "from bs4 import BeautifulSoup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9d8449a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第1页爬完\n",
      "第2页爬完\n",
      "第3页爬完\n",
      "第4页爬完\n",
      "第5页爬完\n",
      "第6页爬完\n",
      "第7页爬完\n",
      "第8页爬完\n",
      "第9页爬完\n",
      "第10页爬完\n",
      "第11页爬完\n",
      "第12页爬完\n",
      "第13页爬完\n"
     ]
    }
   ],
   "source": [
    "car_list = []  #车名\n",
    "#price_ori = []  #原价\n",
    "price_pre = []  #现价\n",
    "pay_list = []  #首付\n",
    "price_down = []  #降价\n",
    "mile_list = []  #行程\n",
    "city_list = []  #城市\n",
    "time_list = []  #天数\n",
    "transfer_list = []  #过户\n",
    "\n",
    "\n",
    "n_page = 20  #爬取页数\n",
    "\n",
    "for i in range(n_page):\n",
    "    url = f'https://www.renrenche.com/cn/ershouche/p{i+1}/'\n",
    "    res = requests.get(url)\n",
    "    \n",
    "    page=BeautifulSoup(res.text,'lxml')\n",
    "    li_list = page.find('ul',class_='row-fluid list-row js-car-list').find_all('li')\n",
    "    #li_list = page.find('ul',class_='row-fluid list-row js-car-list').find_all('li',class_='span6 list-item car-item ')\n",
    "    #print(li_list)\n",
    "    for li in li_list:\n",
    "        #print(li)\n",
    "        try:\n",
    "            car = li.find('h3',class_='rrcttfbd3d9d4e00c260c8229a2b562f527bd0').text\n",
    "            car_list.append(car)\n",
    "            #print(car)\n",
    "        except:\n",
    "            #print('---')\n",
    "            continue\n",
    "        \n",
    "        price = li.find('div',class_='tags-box').find('div',class_='price')\n",
    "        pre = price.text.strip().split('  ')[0]\n",
    "        price_pre.append(pre)\n",
    "        \n",
    "        #首付\n",
    "        try:\n",
    "            pay_list.append(price.find('div',class_='down-payment').get_text())\n",
    "        except:\n",
    "            pay_list.append('0')\n",
    "        \n",
    "        basic = li.find('span',class_='basic rrcttfbd3d9d4e00c260c8229a2b562f527bd0').get_text().split('/')\n",
    "        time_list.append(basic[0])\n",
    "        mile_list.append(basic[1])\n",
    "        city_list.append(basic[2])\n",
    "        \n",
    "        #降价\n",
    "        try:\n",
    "            down = li.find('div',class_='dis-main').get_text()\n",
    "            price_down.append(down)\n",
    "        except:\n",
    "            price_down.append('0')\n",
    "        \n",
    "        #过户\n",
    "        try:\n",
    "            tran = li.find('div',class_='mileage-tag-box').find_all('span',class_='tags')[-1].text\n",
    "            print(tran)\n",
    "            transfer_list.append(tran)\n",
    "        except:\n",
    "            transfer_list.append('未知')\n",
    "            \n",
    "    print(f'第{i+1}页爬完')\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "2e435228",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from pandas import DataFrame\n",
    "\n",
    "df = DataFrame({\n",
    "    '城市':city_list,\n",
    "    '车型':car_list,\n",
    "    '现价':price_pre,\n",
    "    '首付':pay_list,\n",
    "    '降价':price_down,\n",
    "    '里程':mile_list,\n",
    "    '年份':time_list,\n",
    "    '过户':transfer_list})\n",
    "\n",
    "df.to_excel('car_20.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51c71bb5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}